#!/usr/bin/env python
# -*-coding:utf-8-*-
# File    : liyuqi.py

import os
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.action_chains import ActionChains

# 下载设置
chrome_options = Options()
# prefs = {'profile.default_content_settings.popups': 0,
#          'download.default_directory': os.getcwd()}
#
#
# chrome_options.add_experimental_option('prefs', prefs)

chrome_options.add_experimental_option("debuggerAddress", "127.0.0.1:9222")
driver = webdriver.Chrome(options=chrome_options)

driver.get("https://image.baidu.com/search/index?tn=baiduimage&ps=1&ct=201326592&lm=-1&cl=2&nc=1&ie=utf-8&word=%E6%B7%B1%E5%9C%B3")

time.sleep(3)
#定位图片元素
images = lambda x:x.find_elements_by_css_selector('div.imgbox')

#当页面获取到的图片数少于50，则滚动鼠标中键
while len(images(driver)) < 50:
    #通过调用js来滚动鼠标中键
    driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
    time.sleep(1)

#点击第49张图片
images(driver)[49].click()